#pragma once

// Dependencies
#include "Platform.h"
#include "Point2.h"
#include "Vector2.h"
#include "Polygon2.h"
#include "Contour2.h"
#include <vector>
#include <algorithm>

class WorldGeometry {
private: // Helper structures
	struct IndexedEdge {
		unsigned idxA;
		unsigned idxB;
		Vector2 normal;
	};
	
public: // Structures
	struct EdgeInfo { 
		int idxPolygon;
		int idxContour;
		int idxEdge;
	};

	struct IntersectionResult {
		Point2 intersection;
		Vector2 normal;
		double sqDistance;
		const EdgeInfo* extra;
	};

public: // Constructors
	WorldGeometry();

public: // Methods
	void compile( const Polygon2* polys, size_t ct );
	bool trace( const Point2& pt, const Vector2& dir, IntersectionResult& result ) const;
	int addPolygon( const Polygon2& poly );
	
private: // Members
	std::vector<Point2> mVertices;
	std::vector<IndexedEdge> mEdges;
	std::vector<EdgeInfo> mEdgeInfo;
	int mPolygonCount;

	friend struct IndexedEdge;
};